var express = require('express');
var router = express.Router();
const Blog = require('../connet/mongod')

router.get('/', async (req, res)=>{
  try {
    const data = await Blog.like.find()
    res.status(200).send({data,isok: true})
  } catch (error) {
    console.log(error, '错误');
  }
});
// 新增
router.post('/addlike', async (req, res)=>{
  try {  
    const likes = {title: req.body.title, text: req.body.text}
    console.log(req.body, '参数');
    const data = await Blog.like.create(likes)
    res.status(200).send({data, isok: true });  
  } catch (error) {  
    console.error('Error inserting like:', error);  
    res.status(500).send({ message: 'An error occurred while inserting like' });  
  }
})
// 删除
router.post('/dellike', async function(req, res){
    const _id = req.body.id
    const data = await Blog.like.deleteOne(_id);
    res.status(200).send({data,isok:true})
  })
//修改
router.post('/update', async function(req, res){
  const query = req.body
  console.log(req.body, '参数');
  const data = await Blog.like.updateOne({_id:query.id}, query);
  res.status(200).send({data,isok:true})
})

module.exports = router;
